package me.lx

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import com.blankj.utilcode.util.ThreadUtils
//import com.tencent.smtt.export.external.interfaces.WebResourceRequest
//import com.tencent.smtt.export.external.interfaces.WebResourceResponse
import com.tencent.smtt.sdk.*
import com.webuy.webview.dsbrige.DWebView
import kotlinx.android.synthetic.main.act_main.*
import me.lx.databinding.ActSecondBinding
import timber.log.Timber


class SecondAct : AppCompatActivity() {
    lateinit var binding: ActSecondBinding
    lateinit var nativeWebView: android.webkit.WebView
    lateinit var tencentWebView: WebView
    lateinit var dWebView: DWebView

    var url = "http://www.maoke123.com/"
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.act_second)
        binding = DataBindingUtil.inflate(LayoutInflater.from(this), R.layout.act_second, null, false)
        setContentView(binding.root)
        initView()
        onClickBtn1()
    }

    private fun initView() {
        nativeWebView = binding.nativeWebView
        tencentWebView = binding.tencentWebView
        dWebView = binding.dWebView
        binding.btn1.setOnClickListener { onClickBtn1() }
        binding.btn2.setOnClickListener { onClickBtn2() }
        binding.btn3.setOnClickListener { onClickBtn3() }
        initSetting(nativeWebView)
        initSetting(tencentWebView)
        initSetting(dWebView)
        initWebViewClient(tencentWebView)
    }

    private fun initWebViewClient(webView: WebView) {
//        webView.webViewClient = object : WebViewClient() {
//            override fun shouldInterceptRequest(
//                view: WebView,
//                request: WebResourceRequest?
//            ): WebResourceResponse? {
//                if (request != null) {
//                    var res :WebResourceResponse?=null
//                    //var res = JlH5Container.interceptRequest(webView.context,request)
//                    // val res = GlideImgCacheManager.interceptRequest(view!!.context, request.getUrl().toString())
//                    Timber.d(
//                        "shouldInterceptRequest()..11..name=${Thread.currentThread().name} isMain=${ThreadUtils.isMainThread()} " +
//                                "res=${res}  request=${request?.url}  size=${request.getRequestHeaders().size}  Headers=${request.getRequestHeaders()}"
//                    )
//                    if (res != null) {
//                        try {
//                           Timber.d("size=${res.responseHeaders.size}  Headers=${res.responseHeaders}")
//                           return res;
//                        } catch (e: Exception) {
//                            e.printStackTrace();
//                        }
//                    }
//                }
//                val oldRes =  super.shouldInterceptRequest(view, request)
//                Timber.d("oldRes size=${oldRes?.responseHeaders?.size}  oldRes=${oldRes?.responseHeaders}")
//                return oldRes
//            }
//
////            override fun shouldOverrideUrlLoading(p0: WebView?, request: WebResourceRequest?): Boolean {
////                Timber.d("shouldOverrideUrlLoading()..3333..request=${request?.url}")
////                return super.shouldOverrideUrlLoading(p0, request)
////            }
//        }
    }



    private fun initSetting(view: Any) {
        when (view) {
            is android.webkit.WebView -> {
                val settings = view.getSettings()
                //默认是false 设置true允许和js交互
                settings.setJavaScriptEnabled(true)
                settings.setCacheMode(WebSettings.LOAD_NO_CACHE)  // 如果h5里的图请求过,第2次加载H5,里面的图片还是有缓存
                settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK)  // 如果h5里的图请求过,第2次加载H5,里面的图片还是有缓存

                //  WebSettings.LOAD_DEFAULT 如果本地缓存可用且没有过期则使用本地缓存，否加载网络数据 默认值
                //  WebSettings.LOAD_CACHE_ELSE_NETWORK 优先加载本地缓存数据，无论缓存是否过期
                //  WebSettings.LOAD_NO_CACHE  只加载网络数据，不加载本地缓存
                //  WebSettings.LOAD_CACHE_ONLY 只加载缓存数据，不加载网络数据
                //Tips:有网络可以使用LOAD_DEFAULT 没有网时用LOAD_CACHE_ELSE_NETWORK
            }
            is com.tencent.smtt.sdk.WebView -> {
                val settings = view.getSettings()
                //默认是false 设置true允许和js交互
                settings.setJavaScriptEnabled(true)
                settings.setCacheMode(WebSettings.LOAD_NO_CACHE)  // 如果h5里的图请求过,第2次加载H5,里面的图片还是有缓存
                settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK)  // 如果h5里的图请求过,第2次加载H5,里面的图片还是有缓存
            }
        }
        
        
     
  
    }


    fun onClickBtn1() {
        loadUrl(nativeWebView)
        loadUrl(tencentWebView)
        loadUrl(dWebView)
    }

    private fun loadUrl(view: Any) {
        // view.clearCache(false) // 即使传false,也可以让h5里的图片再次请求
        when (view) {
            is android.webkit.WebView -> {
                view.loadUrl(UrlConfig.test_url_1360768)
            }
            is com.tencent.smtt.sdk.WebView -> {
                view.loadUrl(UrlConfig.test_url_1360768)
            }
        }
    }

    fun onClickBtn2() {


    }

    fun onClickBtn3() {

    }
}